package com.liang.gulimall.seckill.controller;

import com.liang.common.utils.R;
import com.liang.gulimall.seckill.service.SecKillService;
import com.liang.gulimall.seckill.to.SecKillSkuInfoRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SecKillController {
    @Autowired
    SecKillService secKillService;

    /**
     * 返回当前时间参与秒杀的商品
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getCurrentSecSku")
    public R getCurrentSecSku() {
        List<SecKillSkuInfoRedis> skuInfoRedis = secKillService.getCurrentSecSku();
        return R.ok().setData(skuInfoRedis);
    }

    /**
     * 返回某个的秒杀信息
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/secKillInfo/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId) {
        SecKillSkuInfoRedis skuInfoRedis = secKillService.getSkuSecKillInfo(skuId);
        return R.ok().setData(skuInfoRedis);
    }

    /**
     * 秒杀
     *
     * @param killId
     * @param key
     * @param num
     * @return
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num, Model model) {
        String orderSn = secKillService.kill(killId, key, num);
        model.addAttribute("orderSn",orderSn);
        return "success";

    }
}
